package com.risk.login.service;

import com.risk.login.dao.mapper.LoginUserMapper;
import com.risk.login.dao.po.LoginUserPo;
import com.risk.login.dto.LoginUserDto;
import com.risk.login.dto.common.LoginConstant;
import com.risk.login.exception.BusinessException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @Desc
 * @Date 2018/5/23:下午11:23
 * @uthor yzMa
 * @email 2641007740@qq.com
 */
@Service("loginUserRServiceImpl")
public class LoginUserRService {


    private Logger LOGGER = LoggerFactory.getLogger(LoginUserRService.class);

//    @Autowired
//    private LoginUserRepository loginUserRepository;

    @Autowired
    private LoginUserMapper loginUserMapper;

    private BeanCopier po2DtoCopier = BeanCopier.create(LoginUserPo.class,LoginUserDto.class,false);

    @Autowired
    private RedisTemplate<String,?> redisTemplate;

    private Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(LoginUserDto.class);


    /**
     * 登录框用户名和密码
     * @param loginName
     * @return
     */
    public LoginUserDto checkLogin(String loginName,String passwd) throws BusinessException {
        Assert.notNull(loginName,"checkLogin loginName 不能为空");
        Assert.notNull(passwd,"checkLogin passwd 不能为空");

        LoginUserPo loginUserPo = loginUserMapper.selectByLoginName(loginName);
        if(loginUserPo == null){
            throw new BusinessException("checkLogin-s-1","登录名："+loginName+"不存在");
        }
        if(!StringUtils.equals(loginUserPo.getPasswd(),passwd)){
            throw new BusinessException("checkLogin-s-2","密码错误");
        }
        LoginUserDto loginUserDto = new LoginUserDto();
        po2DtoCopier.copy(loginUserPo,loginUserDto,null);
        return loginUserDto;
    }


    /**
     *
     * @param phone
     * @param passwd
     * @return
     * @throws BusinessException
     */
    public LoginUserDto checkPhone(String phone,String passwd) throws BusinessException {
        Assert.notNull(phone,"checkPhone phone 不能为空");
        Assert.notNull(passwd,"checkPhone passwd 不能为空");

        LoginUserPo loginUserPo = loginUserMapper.selectByPhone(phone);
        if(loginUserPo == null){
            throw new BusinessException("checkLogin-s-1","登录名："+phone+"不存在");
        }
        if(!StringUtils.equals(loginUserPo.getPasswd(),passwd)){
            throw new BusinessException("checkLogin-s-2","密码错误");
        }
        LoginUserDto loginUserDto = new LoginUserDto();
        po2DtoCopier.copy(loginUserPo,loginUserDto,null);
        return loginUserDto;
    }
    /**
     * 根据loginToken检查是否登录
     * @param loginToken
     * @return
     */
    public LoginUserDto checkLoginedByToken(String loginToken) {
        Assert.notNull(loginToken,"token 不能为空");

        String[] split = loginToken.split(":");
        if(split == null || split.length!=3){ //写入的时候就是3个长度
            LOGGER.info("|checkLoginedByToken loginToken={} not illegal",loginToken);
            return null;
        }
        String redisKey = LoginConstant.REDIS_LOGIN_PREFIX+ split[0]+":"+split[1];

        return redisTemplate.execute((RedisCallback<LoginUserDto>) redisConnection -> {
            byte[] data = redisConnection.get(redisKey.getBytes());
            if(data == null){
                return null;
            }
            return (LoginUserDto) jackson2JsonRedisSerializer.deserialize(data);
        });
    }
}
